from PySide2.QtCore import Qt, Signal
from PySide2.QtWidgets import  QHBoxLayout, QVBoxLayout,  QWidget
from dayu_widgets import MLineEdit, MToolButton, MTreeView


class PluginSearch(QWidget):
    update_tree_view_signal = Signal(str)
    expend_tree_view_signal = Signal(str)

    def __init__(self,parent=None):
        super(PluginSearch, self).__init__(parent)
        self.initUI(parent)

    def initUI(self,parent):
        self.search_input = MLineEdit().small()
        self.search_input.setFixedHeight(20)

        remove_btn = MToolButton().svg("close_line.svg").icon_only()
        collapse_btn = MToolButton().svg("big_view.svg").icon_only()
        expend_btn = MToolButton().svg("list_view.svg").icon_only()

        # 事件处理
        self.search_input.returnPressed.connect(self.slot_search)
        remove_btn.clicked.connect(self.slot_remove_text)
        collapse_btn.clicked.connect(self.slot_collapse_plugins)
        expend_btn.clicked.connect(self.slot_expend_plugins)

        search_lay = QHBoxLayout()
        search_lay.addWidget(self.search_input)
        search_lay.addWidget(remove_btn)
        search_lay.addWidget(collapse_btn)
        search_lay.addWidget(expend_btn)

        main_lay = QVBoxLayout()
        main_lay.addLayout(search_lay)
        self.setLayout(main_lay)

    def slot_search(self):
        self.update_tree_view_signal.emit(self.search_input.text())

    def slot_remove_text(self):
        self.search_input.clear()

    def slot_collapse_plugins(self):
        self.expend_tree_view_signal.emit("CLOSE")

    def slot_expend_plugins(self):
        self.expend_tree_view_signal.emit("OPEN")


if __name__ == "__main__":
    # Import local modules
    from dayu_widgets import dayu_theme
    from dayu_widgets.qt import application

    with application() as app:
        test = PluginSearch()
        dayu_theme.apply(test)
        test.show()
